package com.uchengwang.ui.cm_ui.uu.adapter;

import android.app.Activity;
import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.uchengwang.R;
import com.uchengwang.functions.http.config.BaseConfig;
import com.uchengwang.functions.http.listener.HttpCallBack;
import com.uchengwang.functions.image.ImageManager;
import com.uchengwang.ui.UChengApplication;
import com.uchengwang.ui.cm_ui.umsg.http.ClubHttpManager;
import com.uchengwang.ui.cm_ui.uu.bean.ClubInviteBean;
import com.uchengwang.ui.utils.DateUtil;
import com.uchengwang.ui.utils.LoadingUtils;
import com.uchengwang.ui.utils.UCommonUtil;
import com.zhy.autolayout.utils.AutoUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by U城网 on 2017/8/11.
 */

public class ClubInviteAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder>{
    private Context context;
    private List<ClubInviteBean> list = new ArrayList<>();

    public ClubInviteAdapter(Context context){
        this.context = context;
    }

    public void setData(List<ClubInviteBean> list){
        this.list = list;
        notifyDataSetChanged();
    }

    public List<ClubInviteBean> getData(){
        return list;
    }


    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.layout_club_invite, parent, false);
        return new ClubNotifyVH(view);
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder h, int position) {
        ClubNotifyVH holder = (ClubNotifyVH) h;
        final ClubInviteBean inviteBean = list.get(position);

        String name = inviteBean.getOrgName();
        final long orgId = inviteBean.getOrgId();
        final boolean isAccept = inviteBean.isAccept();//是否接受

        long time = inviteBean.getCreateTime();

        holder.invite_time.setText(DateUtil.getDateUtil().getTimeStringDate(time));


        String headUrl = inviteBean.getOrgLogo();
        holder.invite_name.setText(name);
        switch (UChengApplication.userRole){
            case BaseConfig.CLUB_ROLE:
                holder.invite_name.setText("您");
                holder.clubInvite_text.setText("邀请"+inviteBean.getInvitedNickName()+"加入");
                holder.invite_clubName.setText(inviteBean.getOrgName());
                holder.invite_ok.setVisibility(View.GONE);
            break;
            case BaseConfig.STUDENT_ROLE:
                holder.invite_name.setText(inviteBean.getOrgName());
                holder.clubInvite_text.setText("邀请您加入");
                holder.invite_clubName.setText("");
                holder.invite_ok.setVisibility(View.VISIBLE);
                break;
        }

        if (!TextUtils.isEmpty(headUrl)){
            ImageManager.getInstance().loadImage(context, holder.invite_head, headUrl, ImageManager.ImageType.CIRCLE);
        }else {
            ImageManager.getInstance().loadImage(context, holder.invite_head, UChengApplication.userAvatar, ImageManager.ImageType.CIRCLE);
        }

        if (!isAccept){
            if (UChengApplication.userRole == BaseConfig.STUDENT_ROLE){
                holder.invite_ok.setText("同意加入");
                holder.invite_ok.setTextColor(context.getResources().getColor(R.color.release_selector));
            }else {
                holder.invite_ok.setText("待加入");
                holder.invite_ok.setTextColor(context.getResources().getColor(R.color.release_selector));
            }

        }else {
            holder.invite_ok.setText("已加入");
            holder.invite_ok.setTextColor(context.getResources().getColor(R.color.comment_color));
        }

        holder.invite_ok.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (!isAccept && UChengApplication.userRole == BaseConfig.STUDENT_ROLE){
                    LoadingUtils.getUtils().loading(context);
                    ClubHttpManager.getManager().acceptClubInvite((Activity) context, inviteBean.getId(), new HttpCallBack() {
                        @Override
                        public void onError(int status, String msg) {
                            LoadingUtils.getUtils().hide();
                            Log.d("jjf", "status="+status+"=msg="+msg);
                            UCommonUtil.getUtil().showToast(context, "网络异常");
                        }

                        @Override
                        public void onResp(int status, String msg, String data) {
                            LoadingUtils.getUtils().hide();
                            Log.d("jjf", "status="+status+"=msg="+msg);

                            if (status != 0){
                                UCommonUtil.getUtil().showToast(context, "加入失败");
                                return;
                            }
                            UCommonUtil.getUtil().showToast(context, "加入成功");
                            inviteBean.setAccept(true);
                            notifyDataSetChanged();
                        }
                    });
                }
            }
        });
    }

    @Override
    public int getItemCount() {
        return list.size();
    }


    class ClubNotifyVH extends RecyclerView.ViewHolder{
        private View itemView;
        private ImageView invite_head;
        private TextView invite_name, invite_clubName, invite_time, clubInvite_text;
        private Button invite_ok;

        public ClubNotifyVH(View view) {
            super(view);
            AutoUtils.autoSize(view);
            itemView = view;

            invite_time = (TextView) view.findViewById(R.id.clubInvite_time);
            invite_head = (ImageView) view.findViewById(R.id.clubInvite_head);
            invite_name = (TextView) view.findViewById(R.id.clubInvite_name);
            invite_clubName = (TextView) view.findViewById(R.id.clubInvite_clubName);
            clubInvite_text = (TextView) view.findViewById(R.id.clubInvite_text);

            invite_ok = (Button) view.findViewById(R.id.clubInvite_ok);
        }
    }
}
